package org.example;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class TestCtripFlight {
    private WebDriver driver;
    /**
    * 在每个测试方法之前执行的设置方法
    */
    @BeforeEach
    public void setup() {
        //提交最终代码脚本时，请将驱动路径换回官方路径"C:\\Users\\86153\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"
    //        System.setProperty("webdriver.chrome.driver", "C:\\Users\\86153\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
        // 设置ChromeDriver的路径，使用本地路径
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\hxdeng2\\Desktop\\chromedriver.exe");
        // 创建ChromeOptions对象用于配置ChromeDriver
        ChromeOptions chromeOptions = new ChromeOptions();
        // 添加参数以允许远程请求
        chromeOptions.addArguments("--remote-allow-origins=*");
        // 初始化ChromeDriver并传入配置选项
        driver = new ChromeDriver(chromeOptions);
        // 打开指定的URL
        driver.get("https://www.ctrip.com");
        // 最大化浏览器窗口
        driver.manage().window().maximize();
        // 设置隐式等待时间为10秒
        driver.manage().timeouts().implicitlyWait(java.time.Duration.ofSeconds(10));
    
    }
    // test-code-start
    // 请在此处插入JUnit5代码
    public void  test_meth_public(){
        Actions actions =new Actions(driver);
//      导航栏按钮
        WebElement daohangElement= driver.findElement(By.id("leftSideNavLayer"));
        actions.moveToElement(daohangElement).perform();
//       机票
        WebElement jipiaoElement= driver.findElement(By.xpath("//*[@id=\"leftSideNavLayer\"]/div/div/div[2]/div/div[1]/div/div[2]/button/span[2]"));
        actions.moveToElement(jipiaoElement).perform();
        //国内/国际机票
        driver.findElement(By.xpath("//*[@id=\"popup-2\"]/div[2]/a[1]/span")).click();
    }

    //R001
    @ParameterizedTest
    @CsvSource({
            "北京,广州,CtripFlight_R001_001.png",
            "北京,成都,CtripFlight_R001_002.png",
            "上海,广州,CtripFlight_R001_003.png",
            "上海,成都,CtripFlight_R001_004.png"
    })
    public void  test_CtripFlight_R001(String fromFlight,String toFlight, String screen) throws InterruptedException {
        test_meth_public();
        //单程按钮点击
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[1]/ul/li[1]/span")).click();
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[1]/div/div/div[1]/input")).click();
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[1]/div/div/div[1]/input")).sendKeys(Keys.BACK_SPACE);
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[1]/div/div/div[1]/input")).sendKeys(fromFlight);
        Thread.sleep(2000);
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[3]/div/div/div[1]/input")).click();
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[3]/div/div/div[1]/input")).sendKeys(Keys.BACK_SPACE);
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[3]/div/div/div[1]/input")).sendKeys(toFlight);
        Thread.sleep(2000);
//        日期框
        driver.findElement(By.xpath("//*[@id=\"datePicker\"]/div[1]/span/div/div/div/input")).click();
//        选择日期
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[1]/div[2]/div/div[2]/div[5]")).click();
//        带儿童
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[3]/div/div/div/div/div/div/div[1]")).click();
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[1]/div/div/div[1]")).click();
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[1]/div/div/div[2]/ul/li[2]/div[2]")).click();

        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/button")).click();
//        扫码登录时间
        Thread.sleep(20000);
        try {
            // 使用显示等待方法等待元素出现
            WebDriverWait wait = new WebDriverWait(driver, java.time.Duration.ofSeconds(10));
            wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"hp_container\"]/div[2]/div/div[3]/div[2]/div/ul[1]/li[1]/div/span")));
            System.out.println("元素已找到");
        } catch (TimeoutException e) {
            System.out.println("等待超时，未找到元素");
        }
        takeScreenshot(screen);
    }


    @ParameterizedTest
    @CsvSource({
            "**,CtripFlight_R002_001.png",
            "红苹果,CtripFlight_R002_002.png"

    })
    public void  test_CtripFlight_R002(String fromFlight, String screen) throws InterruptedException {
        test_meth_public();
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[1]/ul/li[1]/span")).click();
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[1]/div/div/div[1]/input")).click();
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[1]/div/div/div[1]/input")).sendKeys(Keys.BACK_SPACE);
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[1]/div/div/div[1]/input")).sendKeys(fromFlight);
        Thread.sleep(2000);
        try {
            // 使用显示等待方法等待元素出现
            WebDriverWait wait = new WebDriverWait(driver, java.time.Duration.ofSeconds(10));
            WebElement errorMessage=driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[1]/div/div/div[2]/div[1]"));
            wait.until(ExpectedConditions.textToBePresentInElement(errorMessage,"对不起，暂不支持该地点"));
            System.out.println("元素已找到");
        } catch (TimeoutException e) {
            System.out.println("等待超时，未找到元素");
        }
        takeScreenshot(screen);
    }

    @ParameterizedTest
    @CsvSource({
            "北京,东京,CtripFlight_R003_001.png",
            "北京,大坂,CtripFlight_R003_002.png",
            "上海,东京,CtripFlight_R003_003.png",
            "上海,大坂,CtripFlight_R003_004.png"
    })
    public void  test_CtripFlight_R003(String fromFlight,String toFlight, String screen) throws InterruptedException {
        test_meth_public();
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[1]/ul/li[1]/span")).click();
//        出发地框点击
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[1]/div/div/div[1]/input")).click();
//        出发地框清空
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[1]/div/div/div[1]/input")).sendKeys(Keys.BACK_SPACE);
//        出发地框输入
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[1]/div/div/div[1]/input")).sendKeys(fromFlight);
        Thread.sleep(2000);
//        目的地框点击
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[3]/div/div/div[1]/input")).click();
//        目的地框清空删除
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[3]/div/div/div[1]/input")).sendKeys(Keys.BACK_SPACE);
//        目的地输入
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[1]/div/div[3]/div/div/div[1]/input")).sendKeys(toFlight);
//       点击下拉框
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[3]/div/div/div/div[1]")).click();
//        成人+1
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[3]/div/div/div/div[2]/div[1]/div[2]/div[3]")).click();
//        儿童+1
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[3]/div/div/div/div[2]/div[2]/div[2]/div[3]")).click();
//        婴儿+1
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[3]/div/div/div/div[2]/div[3]/div[2]/div[3]")).click();
//        点击确定
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/div/div/div[2]/div[3]/div/div/div/div[2]/div[4]/a[1]")).click();
//        日期框
        driver.findElement(By.xpath("//*[@id=\"datePicker\"]/div[1]/span/div/div/div/input")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div[1]/div[2]/div/div[2]/div[5]")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//*[@id=\"searchForm\"]/div/button")).click();
        //        扫码登录时间
        Thread.sleep(20000);
        try {
            // 使用显示等待方法等待元素出现
            WebDriverWait wait = new WebDriverWait(driver, java.time.Duration.ofSeconds(10));
            wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"hp_container\"]/div[2]/div/div[3]/div[2]/div/ul[1]/li[1]/div/span")));
            System.out.println("元素已找到");
        } catch (TimeoutException e) {
            System.out.println("等待超时，未找到元素");
        }
        takeScreenshot(screen);
    }


    @AfterEach
    public void teardown() {
        this.driver.quit();
    }

    private void takeScreenshot(String fileName) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HHmmssddSSS");
        String timestamp = dateFormat.format(new Date());
        String timestampedFileName = timestamp + "_" + fileName;
        File screenshotsDir = new File("screenshots");
        if (!screenshotsDir.exists()) {
            screenshotsDir.mkdirs();
        }
        String screenshotFilePath = screenshotsDir.getAbsolutePath() + File.separator + timestampedFileName;
        File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(screenshotFile, new File(screenshotFilePath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}